package com.miracle.java2110.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.miracle.java2110.common.PageVo;
import com.miracle.java2110.common.R;
import com.miracle.java2110.mapper.ServiceMapper;
import com.miracle.java2110.pojo.ServiceType;
import com.miracle.java2110.pojo.Serviced;
import com.miracle.java2110.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ServiceImpl implements MyService {

    @Autowired
    private ServiceMapper serviceMapper;


    @Override
    public PageVo<ServiceType> selectAll(Map<String, Object> param) {
        PageHelper.startPage(Integer.parseInt(param.get("page") + ""), Integer.parseInt(param.get("limit") + ""));

        List<ServiceType> serviceTypes = serviceMapper.selectAll(param);
        PageInfo<ServiceType> serviceTypePageInfo = new PageInfo<>(serviceTypes);
        PageVo<ServiceType> serviceTypePageVo = new PageVo<>();
        serviceTypePageVo.setCode(0);
        serviceTypePageVo.setCount(serviceTypePageInfo.getTotal());
        serviceTypePageVo.setMsg("分页");
        serviceTypePageVo.setData(serviceTypePageInfo.getList());

        return serviceTypePageVo;
    }

    @Override
    public R addServiceType(ServiceType st) {
        int i = serviceMapper.addServiceType(st);
        if (i == 1) {
            return R.ok(200, "添加服务类型");
        } else {
            return R.error(400, "添加失败");
        }
    }

    @Override
    public R deleteById(Integer id) {
        Integer i = serviceMapper.deleteById(id);
        if (i == 1) {
            return R.ok(200, "删除成功");
        } else {
            return R.error(400, "删除失败");
        }
    }

    @Override
    public R updateById(ServiceType st) {
        Integer i = serviceMapper.updateById(st);
        if (i == 1) {
            return R.ok(200, "修改成功");
        } else {
            return R.error(400, "修改失败");
        }
    }

    @Override
    public PageVo<Serviced> selectAllService(Map<String, Object> param) {
        PageHelper.startPage(Integer.parseInt(param.get("page") + ""), Integer.parseInt(param.get("limit") + ""));

        List<Serviced> serviceds = serviceMapper.selectAllService(param);
        PageInfo<Serviced> servicedPageInfo = new PageInfo<>(serviceds);
        PageVo<Serviced> servicedPageVo = new PageVo<>();
        servicedPageVo.setCode(0);
        servicedPageVo.setCount(servicedPageInfo.getTotal());
        servicedPageVo.setMsg("分页");
        servicedPageVo.setData(servicedPageInfo.getList());

        return servicedPageVo;
    }

    @Override
    public R addService(Serviced s) {
        int i = serviceMapper.addService(s);
        if (i == 1) {
            return R.ok(200, "添加成功");
        } else {
            return R.error(400, "添加失败");
        }
    }

    @Override
    public R deleteServiceById(Integer id) {
        Integer i = serviceMapper.deleteServiceById(id);
        if (i == 1) {
            return R.ok(200, "删除成功");
        } else {
            return R.error(400, "删除失败");
        }
    }

    @Override
    public R updateService(Serviced serviced) {
        Integer i = serviceMapper.updateService(serviced);
        if (i == 1) {
            return R.ok(200, "修改成功");
        } else {
            return R.error(400, "修改失败");
        }
    }

    @Override
    public R deleteServiced(int[] array) {
        Integer i = serviceMapper.deleteServiced(array);
        if (i >= 1) {
            return R.ok(200, "删除成功");
        } else {
            return R.error(400, "删除失败");
        }
    }
}
